package com.zpark.updater;

import com.zpark.entity.HistoryData;
import com.zpark.entity.SuccessData;

import java.util.ArrayList;
import java.util.List;

public class InputFeatureUpdater extends Updater {

    private int threshold;//阈值，只存储最近的多少条输入特征

    public InputFeatureUpdater(int threshold) {
        this.threshold = threshold;
    }

    @Override
    public void update(SuccessData successData, HistoryData historyData, UpdaterChain updaterChain) {
        doUpdate(successData.getInputFeatures(),historyData);
        updaterChain.doChain(successData, historyData);
    }

    private void doUpdate(Double[] inputFeatures, HistoryData historyData) {
        List<Double[]> latestInputFeatures = historyData.getLatestInputFeatures();
        if(latestInputFeatures==null || latestInputFeatures.size()==0){
            latestInputFeatures=new ArrayList<>();
        }

        latestInputFeatures.add(inputFeatures);

        if(latestInputFeatures.size()>threshold){
            latestInputFeatures.remove(0);
        }

        historyData.setLatestInputFeatures(latestInputFeatures);
    }
}
